from base.basicFunction import *
from selenium.webdriver.common.by import By


class HomePage:
    def __init__(self, data):
        self.data = data
        self.retry_times = 0

    def change_organization(self):
        logger.info("-" * 30 + "切换组织" + "-" * 30)
        data = self.data
        self.retry_times = 0
        Element((By.XPATH, '//*[@id="staff_min"]/div[2]/div')).click()  # 切换组织
        Element((By.XPATH, '//div[starts-with(text(),"切换组织")]')).click()  # 点击切换组织
        Element((By.XPATH, '//*[@id="staff_max"]/div[6]/div[1]')).click()
        Element((By.XPATH, '//li[@title="%s"]' % data['地市'])).click()  # 选择组织
        sleep(2)
        logger.info("-" * 28 + "切换组织完成" + "-" * 28)

    def send_phone_number(self):
        """
        输入受理号码
        @return:
        """
        logger.info("-" * 30 + "输入号码" + "-" * 30)
        data = self.data
        Element((By.ID, 'LOGIN_NUM')).clear()
        Element((By.ID, 'LOGIN_NUM')).send_keys(data['号码'])  # 输入手机号码
        sleep(2)
        Element((By.ID, 'LOGIN_BTN')).click()
        sleep(2)
        logger.info("-" * 28 + "输入号码完成" + "-" * 28)

    def enter_acceptance_bus(self, business_type=None):
        """
        输入受理业务
        @return:
        """
        logger.info("-" * 30 + "输入受理业务" + "-" * 30)
        data = self.data
        retry_times = 0
        business_code = data['业务编码'] if business_type is None else data[business_type]['业务编码']
        while retry_times < 3:
            try:
                # 输入并搜索业务变更新
                Element((By.ID, 'menu_search')).clear()
                sleep(1)
                Element((By.ID, 'menu_search')).send_keys(business_code)
                sleep(1)
                Element((By.XPATH, '//*[@id="menu_search_list"]/li[1]/div[1]')).click()
                sleep(1)
                switch_to_frame((By.XPATH, '//*[@id="main_ct"]/iframe[3]'))
                sleep(1)
                break
            except Exception:
                retry_times += 1
        logger.info("-" * 28 + "输入受理业务完成" + "-" * 28)

    def identity_authentication(self):
        logger.info("-" * 30 + "身份认证" + "-" * 30)
        data = self.data
        wait_for_load((By.XPATH, '//*[@id="SubmitPart_VerifyModeMain_SELECT_VERIFY_MODE_span"]'))  # 点击身份证确认
        sleep(1)
        Element((By.XPATH, '//*[@id="SubmitPart_VerifyModeMain_SELECT_VERIFY_MODE_float"]/div[2]/div/div/ul/li[3]/div')).click()  # 选择其他证件
        sleep(1)
        Element((By.XPATH, '//*[@id="SubmitPart_VerifyModeMain_CERT_TYPE_span"]')).click()  # 证件类型
        sleep(1)
        Element((By.XPATH, '//*[@id="SubmitPart_VerifyModeMain_CERT_TYPE_float"]//div[text()="营业执照（统一社会信用代码）"]')).click()  # 选择营业执照
        sleep(1)
        Element((By.ID, 'SubmitPart_VerifyModeMain_VERIFY_VALUE')).clear()  # 输入证件号码
        Element((By.ID, 'SubmitPart_VerifyModeMain_VERIFY_VALUE')).send_keys(data['证件号'])  # 修改下证件号沈阳号码
        sleep(1)
        Element((By.XPATH, '//*[@id="SubmitPart_VerifyModeMain_ok"]/span[2]')).click()  # 跳过出口认证
        sleep(1)
        try:
            wd.switch_to.alert.accept()
        except Exception as e:
            pass
        sleep(1)
        is_display((By.XPATH,
                    '//*[starts-with(text(),"系统将提交并保存受理数据，您确认要继续吗？")]/parent::div/parent::div/div[2]/button/span[text()="确定"]'))  # 点击确定
        logger.info("-" * 28 + "身份认证完成" + "-" * 28)
        sleep(4)

    def login(self):
        logger.info("-" * 70 + "登录" + "-" * 70)
        data = self.data
        refresh_page()
        check(self.change_organization, "//*[@id='staff_city' and text()='%s']" % data['地市'])
        check(self.send_phone_number, "//*[@id='logoutButton']")
        logger.info("-" * 68 + "登录完成" + "-" * 68)


def refresh_page():
    logger.info("-" * 30 + "刷新页面" + "-" * 30)
    refresh()
    quit_frame()
    logger.info("-" * 28 + "刷新页面完成" + "-" * 28)


def electronic_visa_waiver():
    logger.info("-" * 70 + "电子免填单" + "-" * 70)
    wait_for_load((By.XPATH, '//*[@id="PrintElect_ElectBtn"]/span[2]'))  # 打印电子免填单
    Element((By.XPATH, '//*[starts-with(text(),"打印电子免填单失败")]/parent::div/parent::div/div[2]/button')).click()  # 免填单打印失败
    Element((By.XPATH, '//*[starts-with(text(),"爱知提供的IE控件加载失败")]/parent::div/parent::div/div[2]/button')).click()  # 东软控件加载失败
    # Element((By.XPATH, '//*[starts-with(text(),"东软提供的IE控件加载失败")]/parent::div/parent::div/div[2]/button')).click()  # 东软控件加载失败
    Element((By.XPATH, '//*[starts-with(text(),"当前订单无纸化工单未保存成功")]/parent::div/parent::div/div[2]/ul/li/div[2]/input')).send_keys('自动化测试自动调试请知晓')  # 填写失败原因
    sleep(1)
    Element((By.XPATH, '//*[starts-with(text(),"当前订单无纸化工单未保存成功")]/parent::div/parent::div/div[2]/div/button')).click()  # 点击确定按钮
    Element((By.XPATH, '//*[starts-with(text(),"失败原因保存成功")]/parent::div/parent::div/div[2]/button')).click()  # 关闭温馨提示
    Element((By.XPATH, '//*[starts-with(text(),"发送满意度调查短信给用户")]/parent::div/parent::div/div[2]/button[2]')).click()  # 关闭温馨提示
    Element((By.XPATH, '//*[starts-with(text(),"恭喜")]/parent::div/parent::div/div[2]/button')).click()  # 点击确定
    logger.info("-" * 68 + "电子免填单完成" + "-" * 68)


def check(func, check_syntax):
    for index, checked_num in enumerate(range(3)):
        verification_pass = is_display((By.XPATH, check_syntax), on_click=False)
        if verification_pass:
            return
        try:
            func()  # 回调
        except Exception as e:
            logger.warning('第%d次校验失败' % (checked_num+1))
